package com.zlx.sharelive.activity;

import android.app.Dialog;
import android.content.Intent;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.zlx.module_base.base_ac.BaseAc;
import com.zlx.module_base.base_api.res_data.BaseBean;
import com.zlx.module_base.base_api.res_data.UpDynamicBean;
import com.zlx.module_base.base_api.util.ApiUtil;
import com.zlx.module_base.base_util.SPUtil;
import com.zlx.module_network.api1.livedata.BaseObserver;
import com.zlx.module_network.api1.livedata.BaseObserverCallBack;
import com.zlx.module_network.bean.ApiResponse;
import com.zlx.sharelive.R;
import com.zlx.sharelive.utils.DecimalInputTextWatcher;
import com.zlx.sharelive.utils.StringUt;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 发布文章
 */
public class TextAc extends BaseAc {

    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.iv_charge)
    ImageView iv_charge;
    @BindView(R.id.ll_et_charge)
    LinearLayout ll_et_charge;
    @BindView(R.id.et_video_money)
    EditText et_video_money;
    @BindView(R.id.et_title)
    EditText et_title;
    @BindView(R.id.et_content)
    EditText et_content;


    private String lat, lng, city;
    private String izFee = "0";

    @Override
    protected int getLayoutId() {
        return R.layout.ac_text;
    }


    @Override
    public void initViews() {
        super.initViews();

        moneyLimit();
    }

    /**
     * 金钱限制
     */
    private void moneyLimit() {
        et_video_money.addTextChangedListener(new DecimalInputTextWatcher(et_video_money, 10, 2));
    }


    @OnClick({R.id.iv_back, R.id.ll_address, R.id.ll_charge, R.id.bt_comm})
    public void setOnClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.ll_address:
                startActivityForResult(new Intent(this, AddressListAc.class), 20);
                break;
            case R.id.ll_charge:
                //是否收费
                if (izFee.equals("0")) {
                    izFee = "1";
                    iv_charge.setImageResource(R.mipmap.up_no_img);
                    ll_et_charge.setVisibility(View.VISIBLE);
                } else {
                    izFee = "0";
                    iv_charge.setImageResource(R.mipmap.up_off_img);
                    ll_et_charge.setVisibility(View.GONE);
                }
                break;
            case R.id.bt_comm:
                if (StringUt.isEmpty(et_title.getText().toString().trim())) {
                    Toast.makeText(this, "标题不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (StringUt.isEmpty(et_content.getText().toString().trim())) {
                    Toast.makeText(this, "内容不可为空", Toast.LENGTH_SHORT).show();
                    return;
                } else if (StringUt.isEmpty(city)) {
                    Toast.makeText(this, "请选择地址", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    if (izFee.equals("0")) {
                        //不收费
                        setDynamic();
                    } else {
                        if (StringUt.isEmpty(et_video_money.getText().toString().trim())) {
                            Toast.makeText(this, "请输入收费金额", Toast.LENGTH_SHORT).show();
                            return;
                        } else if (
                                Double.valueOf(et_video_money.getText().toString().trim()) == 0) {
                            Toast.makeText(this, "请输入有效金额", Toast.LENGTH_SHORT).show();
                            return;
                        } else {
                            //上传
                            setDynamic();
                        }
                    }
                }
                break;
        }
    }

    /**
     * 发布文章或者动态
     */
    private void setDynamic() {
        UpDynamicBean upDynamicBean = new UpDynamicBean();
        upDynamicBean.setLat(lat);
        upDynamicBean.setLng(lng);
        upDynamicBean.setCity(city);
        upDynamicBean.setTitle(et_title.getText().toString().trim());
        upDynamicBean.setType("0");
        upDynamicBean.setIzFee(izFee);
        upDynamicBean.setText(et_content.getText().toString().trim());
        upDynamicBean.setMoney(et_video_money.getText().toString().trim());
        ApiUtil.getServiceApi().setDynamic(SPUtil.getToken(this),
                upDynamicBean).observe(this, new BaseObserver<>(new BaseObserverCallBack<ApiResponse<BaseBean>>() {
            @Override
            public void onSuccess(ApiResponse<BaseBean> data) {
                showSuccess();
                if (null != data) {
                    if (null != data.getData()) {
                        if (data.getData().getCode() == 0) {
                            dialogSuccess();
                        } else {
                            Toast.makeText(TextAc.this, data.getData().getMsg(), Toast.LENGTH_SHORT).show();
                        }
                    }
                }
            }

            @Override
            public void onFinish() {
                super.onFinish();

            }
        }));
    }

    private void dialogSuccess() {
        //1、使用Dialog、设置style
        final Dialog dialog = new Dialog(TextAc.this, R.style.DialogTheme);
        //2、设置布局
        View view = View.inflate(TextAc.this, R.layout.dialog_success, null);
        dialog.setContentView(view);
        dialog.setCanceledOnTouchOutside(false);
        TextView tv_continue = view.findViewById(R.id.tv_continue);
        tv_continue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //继续
                dialog.dismiss();
                lat = "";
                lng = "";
                city = "";
                et_title.setText("");
                et_content.setText("");
                et_video_money.setText("");
                tv_address.setText("");
                ll_et_charge.setVisibility(View.GONE);
            }
        });

        TextView tv_black = view.findViewById(R.id.tv_black);
        tv_black.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //回首页
                dialog.dismiss();
                finish();
            }
        });


        Window window = dialog.getWindow();
        //设置弹出位置
        window.setGravity(Gravity.CENTER);
        //设置弹出动画
//        window.setWindowAnimations(R.style.main_menu_animStyle);
        //设置对话框大小
        window.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        dialog.show();

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable @org.jetbrains.annotations.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == 20) {
            String name = data.getStringExtra("name");
            String address = data.getStringExtra("address");
            lat = data.getStringExtra("lat");
            lng = data.getStringExtra("lng");
            city = data.getStringExtra("city");
            tv_address.setText(name + address);
        }
    }

}

